package org.ethanxu.ftpdownload.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * This is a file transfer sample with the minimum function.
 * Feel free to contact me if you have any question.
 * I'm just behind you. :)
 * <p>
 *
 * @author  EthanXu
 * 
 */
public class Server {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(1111);
		System.out.println("server started..");
		Socket socket = server.accept();
		System.out.println("client entered...");
		File file = new File("D://2.jpg");
		InputStream is = new FileInputStream(file);
		OutputStream out = socket.getOutputStream();
		byte[] b = new byte[64];
		int n;
		while((n = is.read(b)) != -1){
			out.write(b,0,n);
		}
		// As in the project, you could send a first byte[64] as the identifier of the following data
		// the identifier may show the following data type, whether it's file data or just some words putting on screen.
		// then this first byte[64] here is just like a data header... think about it!!! 
		out.close();
		is.close();
		server.close();
	}
}
